<template>
  <div>
    <el-page-header icon="" title="用户管理">
      <template #content>
        <span class="text-large font-600 mr-3">添加用户</span>
      </template>
    </el-page-header>

    <el-row>
      <el-col :span="18">
        <el-alert title="请确认工号无误，一经提交无法修改" type="warning" :closable="false" show-icon />
      </el-col>
    </el-row>
    <el-form ref="userFormRef" :model="userForm" :rules="userFormRules" label-width="80px" class="demo-ruleForm" status-icon>
      <el-row>
        <el-col :span="6">
          <el-form-item label="工号" prop="username">
            <el-input v-model.number="userForm.username" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="密码" prop="password">
            <el-input v-model="userForm.password" type="password" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="姓名" prop="name">
            <el-input v-model="userForm.name" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="9">
          <el-form-item label="性别" prop="gender">
            <el-select v-model="userForm.gender" class="m-2" placeholder="Select" style="width: 100%">
              <el-option v-for="item in genderOptions" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="9">
          <el-form-item label="角色" prop="role">
            <el-select v-model="userForm.role" class="m-2" placeholder="Select" style="width: 100%">
              <el-option v-for="item in roleOptions" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="9">
          <el-form-item label="电话" prop="phone">
            <el-input v-model.number="userForm.phone" />
          </el-form-item>
        </el-col>
        <el-col :span="9">
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="userForm.email" />
          </el-form-item>
        </el-col>
      </el-row>

      <!-- 学生部分 -->
      <el-row v-if="userForm.role === 3">
        <el-col :span="6">
          <el-form-item label="专业" prop="major">
            <el-input v-model="userForm.major" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="班级" prop="classes">
            <el-input v-model.number="userForm.classes" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="入学年份" prop="year">
            <el-input v-model.number="userForm.year" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item label="头像" prop="avatar">
        <Upload :avatar="userForm.avatar" @uploadchange="handleAvatarChange" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm()">添加用户</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { useRouter } from 'vue-router'
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import axios from 'axios'
import upload from '@/util/upload'
import Upload from '@/components/upload/Upload.vue'
const router = useRouter()
const userFormRef = ref()
const userForm = reactive({
  username: null,
  password: '',
  name: '',
  gender: null,
  role: null,
  phone: null,
  email: '',
  avatar: '',
  file: null,

  // 学生部分
  major: '',
  classes: null,
  year: null
})
const userFormRules = reactive({
  username: [
    { required: true, message: '请输入工号', trigger: 'blur' },
    { type: 'number', message: '工号应是数字' }
  ],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
  name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
  gender: [{ required: true, message: '请选择性别', trigger: 'blur' }],
  role: [{ required: true, message: '请选择角色', trigger: 'blur' }],
  phone: [
    { required: true, message: '请输入电话', trigger: 'blur' },
    { type: 'number', message: '电话应是数字' }
  ],
  email: [{ required: true, message: '请输入邮箱', trigger: 'blur' }],

  // 学生部分
  major: [{ required: true, message: '请输入专业', trigger: 'blur' }],
  classes: [
    { required: true, message: '请输入班级', trigger: 'blur' },
    { type: 'number', message: '班级应是数字' }
  ],
  year: [
    { required: true, message: '请输入入学年份', trigger: 'blur' },
    { type: 'number', message: '入学年份应是数字' }
  ]
})
const genderOptions = [
  { label: '男', value: 1 },
  { label: '女', value: 2 }
]
const roleOptions = [
  { label: '管理员', value: 1 },
  { label: '教师', value: 2 },
  { label: '学生', value: 3 }
]
// 选择完头像回调
const handleAvatarChange = (file) => {
  // console.log(file.raw)
  userForm.avatar = URL.createObjectURL(file)
  userForm.file = file
}
const submitForm = () => {
  userFormRef.value.validate(async (valid) => {
    if (valid) {
      // 提交数据到后端
      // console.log(userForm)
      await upload('/api/user/add', userForm)
      ElMessage.success('添加成功！')
      if (userForm.role === 3) {
        await upload('/api/student/add', userForm)
        router.push(`/user-manage/studentlist`)
      } else if (userForm.role === 2) {
        await axios.post('/api/teacher/add', { username: userForm.username })
        router.push(`/user-manage/userlist`)
      } else {
        router.push(`/user-manage/userlist`)
      }
    }
  })
}
</script>

<style lang="scss" scoped>
.demo-ruleForm {
  margin-top: 10px;
}
.el-alert {
  margin-top: 30px;
}
</style>
